﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Com.AMap.Maps.Api;
using Com.AMap.Maps.Api.Overlays;
using Com.AMap.Maps.Api.BaseTypes;
using System.Collections.ObjectModel;
using Com.AMap.Search.API.Options;
using Com.AMap.Search.API.Result;
using Com.AMap.Search.API;
using SQLite;



// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace AmapStart
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            //AMarker markers = new AMarker();
            this.InitializeComponent();


            //AMarker[] markers = new AMarker[30];
            //markers[0] = new AMarker(new ALngLat(120, 30));
            //markers[1] = new AMarker(new ALngLat(120.123, 30.5236));
            //markers[2] = new AMarker(new ALngLat(120.1235412, 29.946739));
            //markers[3] = new AMarker(new ALngLat(120.351, 29.3153412));
            //markers[4] = new AMarker(new ALngLat(119.61635, 31.512421));
            
            

            //you add above
            //for (int i = 0; i < markers.Length; i++)
            //{

            //    //markers[i].IconURI = new Uri("http://api.amap.com/Home/Tpl/default/Public/Images/wp7.png");
               
            //    map.Children.Add(markers[i]);



            //}

            //map.Center = new ALngLat(120, 30.5);
            //map.Zoom = 7;
            //map.MapType = MapType.Aerial;
            //数据文件保存的位置

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db1.sqlite");

            ////打开创建数据库和表

            using (var db = new SQLite.SQLiteConnection(dbPath))
            {

                //创建表

                db.CreateTable<StandardPlots>();

            }
            //sqlite语句简单示例

            //连接数据库

            
            var conn = new SQLite.SQLiteConnection(dbPath);

            //删除
            //conn.DeleteAll<StandardPlots>();

            //插入操作。首先声明一个集合

            ObservableCollection<StandardPlots> Collection = new ObservableCollection<StandardPlots>();
            //单条插入语句

            //conn.Insert(new StandardPlots() { Id = 4222801, Name = "恩施监测点01", Lng=118.31,Lat=29.56, Description = "2011年10月28日设立", ImgUrl = "Assets\\Logo.png" });
            //conn.InsertAll(Collection);
            //conn.Close();
            List<object> list = conn.Query(new TableMapping(typeof(StandardPlots)), "select * from  StandardPlots");
            lt.ItemsSource = list;
            
            
            foreach (object item in list)
            {
                StandardPlots sp=item as StandardPlots;
                map.Children.Add(new AMarker(new ALngLat(sp.Lng,sp.Lat)));
                
            }

            map.Center = new ALngLat(112, 31);
            map.Zoom = 8;
            
        }

        private void lt_ItemClick(object sender, ItemClickEventArgs e)
        {
            StandardPlots sp = e.ClickedItem as StandardPlots;
            map.Center = new ALngLat(sp.Lng, sp.Lat);
            map.Zoom = 15;

            ATip tip = new ATip();  //初始化一个信息窗口实例  
            tip.Title = sp.PlotName;   //设置信息窗口的标题  
            tip.ContentText = sp.Description;  //设置信息窗口的内容  

            AMarker marker = new AMarker(); //初始化一个点标注  
            marker.LngLat = map.Center; //设置点标注的经纬度  
            marker.TipFrameworkElement = tip;  //将信息窗口赋值给marker  
            map.Children.Add(marker);//添加点标注对象  
            marker.OpenTip();  //打开信息窗口  

        }

    }

}
